package com.tsvetanv.data.binding.nested.collections.set;

import java.util.Iterator;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.bind.ServletRequestDataBinder;

import com.tsvetanv.data.binding.model.nested.Name;
import com.tsvetanv.data.binding.model.nested.NestedSetCommandBean;

import junit.framework.TestCase;

public class NestedSetBindTest extends TestCase {

	private NestedSetCommandBean bean;
	private ServletRequestDataBinder binder;
	private MockHttpServletRequest request;

	@Override
	protected void setUp() throws Exception {
		bean = new NestedSetCommandBean();
		binder = new ServletRequestDataBinder(bean, "beanName");
		request = new MockHttpServletRequest();
	}

	public void testNestedSetNamesBind() {
		request.addParameter("names[1].firstName", "Tsvetan");
		request.addParameter("names[0].lastName", "Vasilev");

		binder.bind(request);

		assertEquals("Vasilev", bean.getNames().iterator().next().getLastName());
		assertNull(bean.getNames().iterator().next().getFirstName());

		Iterator<Name> iterator = bean.getNames().iterator();
		iterator.next();
		Name secondName = iterator.next();
		assertEquals("Tsvetan", secondName.getFirstName());
		assertNull(secondName.getLastName());
	}

	public void testNestedSetStringsBind() {
		request.addParameter("strings[0]", "Tsvetan");

		// binder.bind(request); // error at this point

		assertTrue(true);
	}
}
